import i18n from "@root/i18n";

// 由于服务端中用户群体广泛，来源多样，建议在每个 t 函数中显式传入当前用户的语言标识，以确保多语言内容能够正确匹配用户所需的语言版本。
console.log("(server)：", i18n.t("welcome_game", { lng: "zh-CN" }));
console.log("(server)：", i18n.t("welcome_ap", { lng: "en" }));

// https://dao3.fun/edit/d9b4a69ca6e265319f05 极限梦魇模式创作端
// https://dao3.fun/edit/376d74093395c208af9e 恐怖修女
console.clear()
// let voxelDict = [];

// for (let x = 0; x < 128; x++) {
//     for (let y = 0; y < 128; y++) {
//         for (let z = 0; z < 128; z++) {
//             let voxelId = voxels.getVoxelId(x, y, z);
//             if (voxelId !== 0) {voxelDict.push([x, y, z,voxelId]);}
//         }
//     }
// }

// console.log(voxelDict);
import './InteractThings.ts'
import './InteractBed.ts'
import './Enemies.ts'
import './keys.ts'


world.onPlayerPurchaseSuccess(async ({ userId, productId, orderId }) => {
    // 假设玩家购买了ID为商品id的商品
    if (String(productId) == '302455011205707') {
        // 由于可能存在多服的情况，每个服都会收到此购买成功的回调，因此需要判断当前服务器有此玩家
        const entity = world.querySelectorAll('player').filter(e => e.player.userId === userId)[0];
        if (entity) {
            // 获得的奖励
            entity.wq = 100; // 假设wq属性是entity对象的一个属性
            entity.player.dialog({
                type: GameDialogType.SELECT,
                title: i18n.t("mall_title", { lng: entity.player.language }),
                content: i18n.t("mall_content", { lng: entity.player.language }),
                options: [i18n.t("mall_option", { lng: entity.player.language })]
            });
        }
    }
});

// world.querySelectorAll('player').forEach((e) => {e.player.link('https://dao3.fun/edit/8a2109ec58264c4f2805')})
// world.querySelectorAll('player').forEach((e) => {e.player.link('https://box3.codemao.cn/e/66088fd78d34bbc9c8d4')})
// world.querySelectorAll('player').forEach((e) => {e.removeTag('吸引')})

world.querySelectorAll('.invisible').forEach((e) => { e.meshInvisible = true })

world.gravity = -0.14

world.onPlayerJoin(async ({ entity }) => {
    // 初始化基本属性
    entity.wq = 0
    entity.addTag('notree')
    entity.position.copy(entity.player.spawnPoint)
    entity.enableDamage = true
    entity.player.runSpeed = 0
    entity.player.cameraMode = GameCameraMode.FPS
    entity.player.enableDoubleJump = false
    entity.addTag('nobridge')
    entity.day = 1

    // 语言选择对话框
    const languageResult = await entity.player.dialog({
        type: GameDialogType.SELECT,
        title: "请选择游戏语言 / Please select game language",
        content: "选择您想要使用的游戏语言 / Choose the language you want to use",
        options: ["中文", "English"],
    });

    // 设置玩家语言
    if (!languageResult || languageResult == null) {
        // 如果没有选择，默认为中文
        entity.player.language = "zh-CN";
    } else {
        if (languageResult.value === "中文") {
            entity.player.language = "zh-CN";
        } else if (languageResult.value === "English") {
            entity.player.language = "en";
        }
    }

    // 根据玩家选择的语言设置初始手持物品文本
    entity.onHand = entity.player.language === 'en' ? 'Nothing' : '空气'

    // 发送欢迎消息
    entity.player.directMessage(i18n.t("welcome_message", { lng: entity.player.language }))

    // 显示提示对话框
    await entity.player.dialog({
        type: GameDialogType.TEXT,
        title: i18n.t("reminder_title", { lng: entity.player.language }),
        content: i18n.t("reminder_content_1", { lng: entity.player.language }),
    })
    await entity.player.dialog({
        type: GameDialogType.TEXT,
        title: i18n.t("reminder_title", { lng: entity.player.language }),
        content: i18n.t("reminder_content_2", { lng: entity.player.language }),
    })
    await entity.player.dialog({
        type: GameDialogType.TEXT,
        title: i18n.t("guide_title", { lng: entity.player.language }),
        content: i18n.t("guide_content", { lng: entity.player.language }),
    })

    // 相机设置
    const do_you = await entity.player.dialog({
        type: GameDialogType.SELECT,
        title: i18n.t("camera_title", { lng: entity.player.language }),
        content: i18n.t("camera_content", { lng: entity.player.language }),
        options: [i18n.t("camera_option_1", { lng: entity.player.language }), i18n.t("camera_option_2", { lng: entity.player.language })]
    })
    if (!do_you || do_you == null) return;
    switch (do_you.index) {
        case 0:
            break;
        case 1:
            entity.player.cameraFovY = 20.5;//开启广角
            break;
        default:
            break;
    }

    await entity.player.dialog({
        type: GameDialogType.TEXT,
        content: i18n.t("day_content", { lng: entity.player.language, day: entity.day }),
    })

    // 玩家交互设置
    entity.player.onPress(async ({ entity, button }) => {
        if (button == 'action1') {
            await entity.player.dialog({
                type: GameDialogType.SELECT,
                title: i18n.t("menu_title", { lng: entity.player.language }),
                content: i18n.t("menu_content", { lng: entity.player.language, item: entity.onHand }),
                options: entity.bq,
            })
        }
    })

    // 特殊玩家皮肤设置
    if (entity.player.name == '坦率的血翼蝠5801' || entity.player.name == '暗23') {
        entity.player.setSkinByName('sans');
    }
})

let code = world.querySelector('#公告栏-1')
if (code) {
    code.enableInteract = true
    code.interactRadius = 2.5
    code.interactHint = i18n.t("code_hint", { lng: "en" });
    code.onInteract(async ({ entity }) => {
        entity.player.link('https://dao3.fun/play/90377378b4f8c01eec95?from_dream')
    })
}

let code1 = world.querySelector('#一张纸条-1')
if (code1) {
    code1.enableInteract = true
    code1.interactRadius = 2.5
    code1.interactHint = i18n.t("note_hint", { lng: "en" });
    code1.onInteract(({ entity }) => {
        entity.player.dialog({
            type: GameDialogType.TEXT,
            title: i18n.t("note_title", { lng: entity.player.language }),
            content: i18n.t("note_content", { lng: entity.player.language }),
        });
    })
}

world.rainSizeHi = 0.2
// world.maxFog = 0.5;
// world.fogColor = new GameRGBColor(1,1,1);
// world.fogHeightOffset = -0.5;
// world.fogStartDistance = 0.5;
// world.fogUniformDensity = 1;
// world.fogHeightFalloff = 0;

